<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>友链</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no"/>
    <meta name="renderer" content="webkit"/>
    <meta name="force-rendering" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <link rel="stylesheet" href="/static/admin/static/component/pear/css/pear.css" />
    <link href="/static/public/libs/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
</head>
<body>
    <form class="layui-form" lay-filter="addForm">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-input-inline"  style="display: none;">
                    <select lay-verify="" type="hidden"></select>
                </div>
                <div class="layui-form-item">
                    <input type="hidden" value="#(link.id)" name="id">
                    <label class="layui-form-label">
                        <span class="p-form-required">*</span>网站名:
                    </label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入网站名" autocomplete="off"
                               class="layui-input" value="#(link.name)">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="p-form-required">*</span>网站地址:
                    </label>
                    <div class="layui-input-block">
                        <input type="text" name="address" required lay-verify="required" placeholder="请输入网站地址" autocomplete="off"
                               class="layui-input" value="#(link.address)">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        网站描述:
                    </label>
                    <div class="layui-input-block">
                         <textarea name="desc" placeholder="请输入网站描述"
                                   class="layui-textarea article-textarea" style="resize: none">#(link.desc??)</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        网站logo:
                    </label>
                    <div class="layui-input-block">
                        <input type="text" name="logo" placeholder="请输入网站logo地址" autocomplete="off"
                               class="layui-input" value="#(link.logo??)">
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="button" lay-submit lay-filter="addForm" class="pear-btn pear-btn-primary pear-btn-sm">
                    <i class="fa fa-check" aria-hidden="true"></i> 确定
                </button>
                <button type="button" class="pear-btn pear-btn-sm p-cancel-btn">
                    <i class="fa fa-close" aria-hidden="true"></i> 取消
                </button>
            </div>
        </div>
    </form>

<script src="/static/public/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="/static/admin/static/component/pear/pear.js"></script>
<script>
    let form, element, layer,$;
    layui.use(['layer', 'form', 'element', 'jquery'], function () {
        form = layui.form;
        element = layui.element;
        layer = layui.layer;
        $ = layui.jquery;
        // 表单验证
        form.verify({});
        // 表单提交
        form.on('submit(addForm)', function (data) {
            $.ajax({
                type: "POST",
                url: "/admin/link/update",
                contentType: "application/json",
                data: JSON.stringify(data.field),
                success: function (data) {
                    if (data.code === 200) {
                        parent.queryTable();
                        parent.parent.toast.success({message: "更新成功",position: 'topCenter'});
                        const index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else {
                        parent.parent.toast.error({message: data.msg,position: 'topCenter'});
                    }
                },
                error: function (data) {
                    parent.parent.toast.error({message: "更新失败",position: 'topCenter'});
                }
            });
            return false;
        });

        // 取消
        $(".p-cancel-btn").click(function () {
            const index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
    });

</script>
</body>
</html>